﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;
using System.IO;

namespace MVC.Controllers
{ 
    public class FotoController : Controller
    {
        private JardimEntities db = new JardimEntities();

        //
        // GET: /EditFotos/

        public ViewResult Index()
        {
            return View(db.Fotos.ToList());
        }

        //
        // GET: /EditFotos/Details/5

        public ViewResult Details(int id)
        {
            Foto foto = db.Fotos.Find(id);
            return View(foto);
        }

        //
        // GET: /EditFotos/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /EditFotos/Create

        [HttpPost]
        public ActionResult Create(Foto foto)
        {
            if (ModelState.IsValid)
            {
                db.Fotos.Add(foto);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(foto);
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/assets/"), fileName);
                file.SaveAs(path);
            }
            // redirect back to the index action to show the form once again
            return RedirectToAction("Index");
        }
        
        //
        // GET: /EditFotos/Edit/5
 
        public ActionResult Edit(int id)
        {
            Foto foto = db.Fotos.Find(id);
            return View(foto);
        }

        //
        // POST: /EditFotos/Edit/5

        [HttpPost]
        public ActionResult Edit(Foto foto)
        {
            if (ModelState.IsValid)
            {
                db.Entry(foto).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(foto);
        }

        //
        // GET: /EditFotos/Delete/5
 
        public ActionResult Delete(int id)
        {
            Foto foto = db.Fotos.Find(id);
            return View(foto);
        }

        //
        // POST: /EditFotos/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Foto foto = db.Fotos.Find(id);
            db.Fotos.Remove(foto);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}